home *** CD-ROM | disk | FTP | other *** search
/ C & C++ Multimedia Cyber Classroom / C and C++ Multimedia Cyber Classroom (Prentice Hall) (1998).iso / src / fig09_05.jar / Ch09 / Fig09_05 / Employ.cpp next >
C/C++ Source or Header  |  1997-10-26  |  953b  |  33 lines

  1. // Fig. 9.5: employ.cpp
  2. // Member function definitions for class Employee
  3. #include <string.h>
  4. #include <iostream.h>
  5. #include <assert.h>
  6. #include "employ.h"
  7.  
  8. // Constructor dynamically allocates space for the
  9. // first and last name and uses strcpy to copy
  10. // the first and last names into the object.
  11. Employee::Employee( const char *first, const char *last )
  12. {
  13.    firstName = new char[ strlen( first ) + 1 ];
  14.    assert( firstName != 0 ); // terminate if not allocated
  15.    strcpy( firstName, first );
  16.  
  17.    lastName = new char[ strlen( last ) + 1 ];
  18.    assert( lastName != 0 );  // terminate if not allocated
  19.    strcpy( lastName, last );
  20. }
  21.  
  22. // Output employee name
  23. void Employee::print() const
  24.    { cout << firstName << ' ' << lastName; }
  25.  
  26. // Destructor deallocates dynamically allocated memory
  27. Employee::~Employee()
  28. {
  29.    delete [] firstName;   // reclaim dynamic memory
  30.    delete [] lastName;    // reclaim dynamic memory
  31. }
  32.  
  33.